//
//  YXDrawerViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/21.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXDrawerViewController.h"

@interface YXDrawerViewController ()

@property (nonatomic ,strong) UIViewController *showingVc;

// 遮盖按钮
@property (nonatomic ,strong) UIButton *coverBtn;

// 左边菜单控制器最大显示范围
@property (nonatomic ,assign) CGFloat leftWidth;

// 主控制器
@property (nonatomic ,strong) UIViewController *mainVc;

// 左边菜单控制器
@property (nonatomic ,strong) UIViewController *leftMenuVc;
@end

@implementation YXDrawerViewController


/**
 获得抽屉控制器
 
 @return 抽屉控制器
 */
+ (instancetype)shareDrawer {
    
    return (YXDrawerViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
}


/**
 快速创建抽屉控制器
 
 @param mainVc   主控制器
 @param leftMenuVc 左边菜单控制器
 @param leftWidth 左边菜单控制器显示的最大范围
 @return 抽屉控制器
 */
+ (instancetype)drawerVcWithMainVC:(UIViewController *)mainVc leftMenuVc:(UIViewController *)leftMenuVc leftWidth:(CGFloat)leftWidth {
    YXDrawerViewController *drawerVc = [[YXDrawerViewController alloc] init];
    drawerVc.mainVc = mainVc;
    drawerVc.leftWidth = leftWidth;
    drawerVc.leftMenuVc = leftMenuVc;
    [drawerVc.view addSubview:leftMenuVc.view];
    [drawerVc.view addSubview:mainVc.view];
    
    [drawerVc addChildViewController:leftMenuVc];
    [drawerVc addChildViewController:mainVc];
    
    return drawerVc;
    
}

/**
 打开左边菜单控制器
 */
- (void)openLeftMenu {
    
    [UIView animateWithDuration:0.25 delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^{
        self.mainVc.view.transform = CGAffineTransformMakeTranslation(self.leftWidth, 0);
        self.leftMenuVc.view.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        // 在mainVc的View添加遮盖按钮
        [self.mainVc.view addSubview:self.coverBtn];
    }];
}

#pragma mark - 懒加载遮盖按钮
- (UIButton *)coverBtn {
    if (!_coverBtn) {
        _coverBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _coverBtn.backgroundColor = [UIColor py_colorWithHexString:@"#fffff" alpha:0.3];
        _coverBtn.frame = self.mainVc.view.bounds;
        
        // 监听按钮的点击
        [_coverBtn addTarget:self action:@selector(coverBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        // 创建一个拖拽手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panCoverBtnAction:)];
        [_coverBtn addGestureRecognizer:pan];
    }
    return _coverBtn;
}

- (void)coverBtnAction {
    [UIView animateWithDuration:0.25 delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^{
        self.mainVc.view.transform = CGAffineTransformIdentity;
        self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth,0);
    } completion:^(BOOL finished) {
        // 移除遮盖按钮
        [self.coverBtn removeFromSuperview];
        self.coverBtn = nil;
    }];
    
}


/**
 监听遮盖按钮的拖拽手势
 */
- (void)panCoverBtnAction:(UIPanGestureRecognizer *)pan {
    CGFloat offsetX = [pan translationInView:pan.view].x;
    if (offsetX > 0) return;
    
    CGFloat distance = self.leftWidth - ABS(offsetX); // 300 - 100 = 200
    
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        if (self.mainVc.view.frame.origin.x > kScreenWidth * 0.5) {
            [self openLeftMenu];
        }else {
            [self coverBtnAction];
        }
    }else if (pan.state == UIGestureRecognizerStateChanged) {
        self.mainVc.view.transform = CGAffineTransformMakeTranslation(MAX(0, distance), 0);
        self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth +  distance, 0);
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 默认左边控制器的view向左偏移
    self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth, 0);
    
    // 添加mainVc的View设置阴影效果
    self.mainVc.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.mainVc.view.layer.shadowOffset = CGSizeMake(-3, -3);
    self.mainVc.view.layer.shadowOpacity = 0.2;
    self.mainVc.view.layer.shadowRadius = 5;
    
    [self addScreenEdgePanGestureRecognizerToView:self.mainVc.view];
}


/**
 给指定的view添加边缘拖拽手势

 @param view mainVc中的View
 */
- (void)addScreenEdgePanGestureRecognizerToView:(UIView *)view {
    // 创建边缘拖拽手势对象
    UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGestureRecognizer:)];
    
    pan.edges = UIRectEdgeLeft;
    
    // 添加手势
    [view addGestureRecognizer:pan];
}

- (void)edgePanGestureRecognizer:(UIScreenEdgePanGestureRecognizer *)pan {
    
    // 获得x方向拖拽的距离
    CGFloat offsetX = [pan translationInView:pan.view].x;
    
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        if (self.mainVc.view.frame.origin.x > kScreenWidth * 0.5) {
            [self openLeftMenu];
        }else {
            [self coverBtnAction];
        }
    }else if (pan.state == UIGestureRecognizerStateChanged) {
        self.mainVc.view.transform = CGAffineTransformMakeTranslation(offsetX, 0);
        self.leftMenuVc.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth +  offsetX, 0);
        
    }
}


/**
 切换到指定的控制器
 */
- (void)switchViewController:(UIViewController *)destVc {
    destVc.view.frame = self.mainVc.view.bounds;
    destVc.view.transform = self.mainVc.view.transform;
    
    [self.view addSubview:destVc.view];
    [self addChildViewController:destVc];
    self.showingVc = destVc;
    [self coverBtnAction];
    [UIView animateWithDuration:0.25 animations:^{
        destVc.view.transform = CGAffineTransformIdentity;
    }];
}

/**
 回到主界面
 */
- (void)backHome {
    [UIView animateWithDuration:0.25 animations:^{
        self.showingVc.view.transform = CGAffineTransformMakeTranslation(self.view.frame.size.width, 0);
    } completion:^(BOOL finished) {
        [self.showingVc removeFromParentViewController];
        [self.showingVc.view removeFromSuperview];
        self.showingVc = nil;
    }];
    
}

@end
